/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.sys.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.sys.dao.SysKeyValueDao
import com.gitee.minimalismstyle.fresh.sys.api.model.SysKeyValue
import com.gitee.minimalismstyle.fresh.sys.api.model.SysKeyValues
import com.gitee.minimalismstyle.fresh.sys.api.service.SysKeyValueService
import org.springframework.beans.BeanUtils
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Service

/**
 * 字典业务实现
 * @author maochao
*/
@Primary
@Service
class SysKeyValueServiceImpl: SysKeyValueService, LongModelBaseServiceImpl<SysKeyValueDao, SysKeyValue>() {

    override fun findByParentId(parentId: Long): R<List<SysKeyValue>> {
        return R.success(dao.findByParentId(parentId))
    }


    override fun findByParentKeyPath(parentKeyPath: String): R<List<SysKeyValue>> {
        val r = findByKeyPath(parentKeyPath)
        return if(r.success){
            val sysKeyValueList = dao.findByParentId(r.get().id!!)
            R.success(sysKeyValueList)
        }else{
            R.error(r.message)
        }
    }

    override fun findByKeyPath(keyPath: String): R<SysKeyValue> {
        var keyValue: SysKeyValue? = dao.findByKeyPath(keyPath)
        return if(keyValue == null){
            R.error("没有查询到，请检查路径是否正确")
        }else{
            R.success(keyValue)
        }
    }

    override fun findSysKeyValuesByKeyPath(keyPath: String): R<SysKeyValues> {
        val sysKeyValues = SysKeyValues()
        val sysKeyValue = findByParentKeyPath(keyPath).get()
        BeanUtils.copyProperties(sysKeyValue, sysKeyValues)
        setChildren(sysKeyValues)
        return R.success(sysKeyValues)
    }

    private fun setChildren(sysKeyValues: SysKeyValues){
        val sysKeyValueList = dao.findByParentId(sysKeyValues.id!!)
        sysKeyValues.children = sysKeyValueList.map {
            val child = SysKeyValues()
            BeanUtils.copyProperties(it, child)
            setChildren(child)
            child
        }
    }

    override fun saveSysKeyValues(sysKeyValues: SysKeyValues): R<SysKeyValues> {
        val isNew = sysKeyValues.id == null
        dao.save(sysKeyValues)
        if(!isNew){
            setChildren(sysKeyValues)
        }
        return R.success(sysKeyValues)
    }

    override fun deleteSysKeyValuesById(id: Long): R<Int> {
        val sysKeyValue = dao.findById(id)
        dao.deleteById(id)
        dao.deleteChildrenByKeyPath(sysKeyValue.keyPath!!)
        return R.success(1)
    }

    private fun addChildrenId(sysKeyValues: SysKeyValues, ids: MutableList<Long>){
        sysKeyValues.children.forEach {
            ids.add(it.id!!)
            addChildrenId(it, ids)
        }
    }

}